package com.example.gpsparse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
public class GpsParseApplication implements CommandLineRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(GpsParseApplication.class);
    
    @Autowired
    private GpsParseService gpsParseService;
    
    @Value("${task.auto-exit:true}")
    private boolean autoExit;
    
    public static void main(String[] args) {
        SpringApplication.run(GpsParseApplication.class, args);
    }
    
    @Override
    public void run(String... args) throws Exception {
        try {
            logger.info("开始执行GPS解析任务...");
            long startTime = System.currentTimeMillis();
            
            // 执行主要业务逻辑
            int processedCount = gpsParseService.processGpsData();
            
            long endTime = System.currentTimeMillis();
            logger.info("GPS解析任务完成，共处理{}条数据，耗时{}毫秒", processedCount, (endTime - startTime));
            
            // 根据配置决定是否自动退出
            if (autoExit) {
                logger.info("任务完成，程序即将退出");
                System.exit(0);
            } else {
                logger.info("任务完成，程序继续运行等待下次调度");
            }
        } catch (Exception e) {
            logger.error("GPS解析任务执行失败", e);
            // 发生异常时退出程序，返回非零状态码
            System.exit(1);
        }
    }
}